<?php
#### LOADER CLASS ##############################################################
class Loader {

	// All loaded items are stored in these vars as objects
	var $loader_model;
	var $loader_library;
	var $loader_module;

	#### HELPER ################################################################
	function helper($name) {
		// require_once is expensive so we put included files in an array for checking
		if (isset($this->included[$name])) return;
		else {
			$this->included[$name] = TRUE;
			return require('includes/helpers/'.$name.'.inc');
		}
	}

	#### MODEL ###############################################################
	function model($name, $require_only = FALSE) {
		if (!isset($this->loader_model->$name)) {
			require('modules/'.$name.'/model.inc');
			if ($require_only) return;
			$call = clean_method_url($name.'Model');
			return $this->loader_model->$name = new $call();
		}
		else return $ref =& $this->loader_model->$name;
	}

	#### LIBRARY ###############################################################
	function library($name, $require_only = FALSE) {
		if (!isset($this->loader_library->$name)) {
			require('includes/libraries/'.$name.'.inc');
			if ($require_only) return;
			$call = clean_method_url($name);
		 	return $this->loader_library->$name = new $call();
		}
		else {

			return $ref =& $this->loader_library->$name;
		}
	}

	#### MODULE LIBRARY ########################################################
	function moduleLibrary($name, $module = FALSE, $require_only = FALSE) {
		if (!isset($this->loader_library->$name)) {
			if (!$module) $module = $this->var['module_name'];
			require('modules/'.$module.'/'.$name.'.inc');
			if ($require_only) return;
			$call = clean_method_url($module.$name);
			return $this->loader_library->$name = new $call();
		}
		else return $ref =& $this->loader_library->$name;
	}

	#### MODULE ################################################################
	function module($name, $require_only = FALSE) {
		if (!isset($this->loader_module->$name)) {
			require('modules/'.$name.'/controller.inc');
			if ($require_only) return;
			$call = clean_method_url($name.'Controller');
			return $this->loader_module->$name = new $call();
		}
		else return $ref =& $this->loader_module->$name;
	}

	#### MODULE ################################################################
	function managementModule($name, $require_only = FALSE) {
		if (!isset($this->loader_module->$name)) {
			require('modules/'.$name.'/management_controller.inc');
			if ($require_only) return;
			$call = clean_method_url($name.'Controller');
			return $this->loader_module->$name = new $call();
		}
		return $ref =& $this->loader_module->$name;
	}

	#### FILE ##################################################################
	function file($file) {
		if (file_exists($file)) include_once($file);
		else die($file.' could not be included');
	}

}
?>